<template>
  <div :class="className" :id="id" :style="{height:height,width:width}" />
</template>
<script>
import echarts from "echarts";
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    linedata: {
      type: Object,
      default: "null"
    },
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "953px"
    },
    height: {
      type: String,
      default: "300px"
    },
    id: {
      type: String,
      default: "chart"
    },
    color: {
      type: String,
      default: '#6E8AC6'
    }
  },
  data() {
    return {
      chart: null,
      line: this.linedata
    };
  },
  watch: {
    'line':{
      handler(newVal, oldVal) {
        if (this.chart) {
          if (newVal) {
            this.initChart(newVal.x, newVal.y)
          } else {
            this.initChart(oldVal.x, oldVal.y)
          }
        } else {
            this.initChart()          
        }
      },
      deep:true
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart(this.line.x, this.line.y)
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(a = [], b = [], c = []) {
      this.chart = echarts.init(document.getElementById(this.id))
      this.chart.setOption({
        color: [this.color],
        tooltip: {
          trigger: "axis"
        },
        legend: {
          show: false,
          icon: "rect",
          bottom: 0,
          button: 0,
          textStyle: {
            fontSize: 12,
            color: "rgba(102,102,102,1)"
          },
          data: []
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "20%",
          containLabel: true
        },
        xAxis: {
          show: false,
          type: "category",
          axisTick: { show: false },
          axisLine: { show: false },
          boundaryGap: false,
          data: a
        },
        yAxis: {
          type: "value",
          show: false,
          axisTick: { show: false },
          axisLine: { show: false }
        },
        series: [
          {
            data: b,
            type: "line",
            areaStyle: {}
          }
        ]
      });
    }
  }
};
</script>
